var fuwuZhanTable;

//定义ztree
var setting ={
			//数据是否采用简单 Array 格式，默认false
			data: {
					simpleData: {
						enable: true
					}
				},
			//在isSimpleData格式下，当前节点id属性
			check: {
				enable: true,
				chkboxType:{ "Y" : "s", "N" : "s" }
			},
			treeNodeKey: "id",
			treeNodeParentKey: "pId",
		};
var zNodes;
/****列表信息*/
function showFuwuZhanList() {
	showLoading("查询中...");
	$.ajax( {
		url : 'fuwuZhan/listPage.do',
		type : 'get',
		async:true,
		dataType : 'html',
		success : function(html) {
			$('#mainForm').html(html);
			queryFuwuzhanDatas();			
			hideLoading();
			
			
			//绑定查询按钮 
		$('#searchFuwuZhanBtn').click(function() {
			showLoading("查询中...");
			queryFuwuzhanDatas();
			hideLoading();
		});
	}
	});
}

function queryFuwuzhanDatas(){
	fuwuZhanTable = $('#entTable').table( {
				width : '100%',
				form : {
					url : 'comm/search.do',
					id : '#entSearchForm',
					type : 'post',
					pageNumberText : '#pageNum'
				},
				pageable : true,
				selectable : true,
				colums : [ {
					columnName : '名称',
					name : 'name',
					handle : "toEditFuwuZhan"
				},  {
					columnName : '登录名',
					name : 'loginName'
				},{
					columnName : '地址',
					name : 'address'
				}, {
					columnName : '联系人',
					name : 'lxr'
				}, {
					columnName : '联系人电话',
					name : 'lxrTel'
				}, {
                    columnName : '创建时间',
                    name : 'crateDate'
                }]
			});
}

/**服务站用户查看编辑页面*/
function showFuwuZhanSelf() {
	$.ajax( {
		url : "fuwuZhan/initPage.do",
		date : 'get',
		dataType : "html",
		success : function(html) {
			$("#commonDialogDiv").html(html);
			$("#commonDialogDiv").dialog( {
				title : "服务站信息维护",
				width : 900,
				height : 600,
				buttons : {
					'确定' : function() {
						saveFuwuZhan();
					},
					'关闭' : function() {
						$(this).dialog('close');
					}
				},close:function(){$(this).dialog('destroy');}
			}
			);
			validator = $("#fuwuZhanForm").validate( {
				rules : {
					"model.name" : {
						required : true,
						maxlength : 20
					},
					"model.detailInfo" : {
						maxlength : 255
					}
				},
				messages : {
					"model.name" : {
						required : "必填"
					}
				}
			});
		}
	});
}


//修改密码
function toResetPwd(){
	var ids = '';
	$('.ids').each(function() {
		if (this.checked == true) {
			ids += 'ids=' + this.value + '&';
		}
	});
	var n=ids.split('&'); 

	if (n.length >2) {
		showDialog('只需要选择一条记录进行修改');
		return;
	} 
	if(ids.length==0){
		showDialog('请选择一条记录进行修改');
		return;
	}
	$.get('fuwuZhan/toResetPwd.do',function(html){
		$('#commonDialogDiv').html(html);
		$('#commonDialogDiv').dialog( {
		modal : true,
		width : 550,
		height : 160,
		buttons : {
			'确定' : function() {
				$(this).dialog('close');
				var pwd=$('#confirmPwd').val();
				$.ajax( {
					url : 'fuwuZhan/doResetPwd.do?password='+pwd + "&" + ids,
					type : 'get',
					dataType : 'json',
					success : function(data) {
						if (data.success) {
							showDialog("修改成功");
							fuwuZhanTable.refresh();
						} else {
							showDialog('修改失败，原因：' + data.message);
						}
					}
				});
			},
			'取消' : function() {
				$(this).dialog('close');
			}
		},close:function(){$(this).dialog('destroy');}
	});
		$("#resetpwdForm").validate({rules:{
					'password':{required:true,rangelength:[6,12]},
					'confirmPwd':{equalTo:'#password'}
				}, messages:{
					'password':{required:"请输入您的新密码！",rangelength:'请输入长度在{0}-{1}位的密码！'},
					'confirmPwd':{equalTo:"两次输入的密码不同！"}
				},submitHandler:function(form){
					form.submit();
				}
			});
	})
}

//去增加
function toEditFuwuZhan(id) {
	var param = '?t=' + ts();
	if (id) {
		param += '&id=' + id;
	}
	$.get('fuwuZhan/toEdit.do' + param, function(html) {
		$('#commonDialogDiv').html(html);
	    initSsxSelect1();
		$('#commonDialogDiv').dialog( {
			modal : true,
			title : '服务站信息表',
			width : 800,
			height : 630,
			buttons : {
				'保存' : function() {
					saveFuwuZhan();
				},
				'取消' : function() {
					$(this).dialog('close');
				}
			},
			open:function(){
				$.ajax({
					async : false,
					cache : false,
					type : 'POST',
					dataType : "json",
					data:{'id':id},
					//请求的action路径
					url : "fuwuZhan/queryTreeNodesJson.do?t="+(new Date()),
					//请求失败处理函数   
					error : function() {
						showDialog('请求失败');
					},
					//请求成功后处理函数。
					success : function(result) {
						//把后台封装好的简单Json格式赋给treeNodes 
						zNodes =  result;
						$.fn.zTree.init($("#tree"), setting, zNodes);
					}
				});
			},close:function(){$(this).dialog('destroy');}
		});
		
		var rules={
				"model.name" : {
					required : true,
				},
				"model.loginName":{
					required:true,remote:{
					url:'fuwuZhan/checkUserId.do'
				}},
				"model.lxr" : {
					required : true,
				},
				"model.address" : {
					required : true,
				},
				"model.lxrTel" : {
					required : true,
				},
				"model.province" : {
					required : true,
				},
				"model.detailInfo":{
					maxlength : 255
				}
			};
		var msgs= {
				"model.name" : {
					required : "必填"
				},
				"model.loginName" : {
					required : "必填",
					remote:"该登录名已经被使用！"
						
				},
				"model.lxr" : {
					required : "必填"
				},
				"model.address" : {
					required : "必填"
				},
				"model.lxrTel" : {
					required : "必填"
				},
				"model.province" : {
					required : "必选"
				},
				"model.detailInfo" : {
					maxlength : "不得超过255个字"
				}
			};
		if(""!=$("#modelId").val()){
			rules['model.loginName'].remote=undefined;
			msgs['model.loginName'].remote=undefined;
		}else{
			rules['model.loginName'].remote={
					url:'fuwuZhan/checkUserId.do'
				};
			msgs['model.loginName'].remote="该登录名已经被使用！";
		}
		validator=$("#fuwuZhanForm").validate({
			rules:rules, 
			messages:msgs,
			submitHandler:function(form){
				form.submit();
			}
		});
	});
}

//去保存
function saveFuwuZhan() {
	var c1 = "";
	var c2 = "";
	var zTree = $.fn.zTree.getZTreeObj("tree");
	$.each(zTree.getCheckedNodes(), function() {
		if(this.level==2){
			c1 += this.id + ",";
			c2 += this.name + ",";
		}
	});
	$("#codes").val(c1);
	$("#areas").val(c2);
	if (validator.form()) {
		$('#fuwuZhanForm').ajaxSubmit( {
			url : 'fuwuZhan/saveFuwuzhan.do',
			type : 'post',
			dataType : 'json',
			success : function(data) {
				if (data.success) {
					showDialog('保存成功');
					$('#commonDialogDiv').dialog('close');
					fuwuZhanTable.refresh();
				} else {
					showDialog('保存失败' + data.message);
				}
			}
		});
	}
}

//批量删除 
function toDeleteFuwuZhan() {
	var ids = '';
	$('.ids').each(function() {
		if (this.checked == true) {
			ids += 'ids=' + this.value + '&';
		}
	});
	if (ids.length == 0) {
		showDialog('请至少选择一条记录！');
		return;
	}
	$('#commonDialogDiv').html('<p>确定要删除记录信息？</p>');
	$('#commonDialogDiv').dialog( {
		modal : true,
		width : 250,
		height : 160,
		buttons : {
			'确定' : function() {
				$(this).dialog('close');
				$.ajax( {
					url : 'fuwuZhan/delete.do?' + ids,
					type : 'get',
					dataType : 'json',
					success : function(data) {
						if (data.success) {
							fuwuZhanTable.refresh();
						} else {
							showDialog('删除失败，原因：' + data.message);
						}
					}
				});
			},
			'取消' : function() {
				$(this).dialog('close');
			}
		},close:function(){$(this).dialog('destroy');}
	});
}

function jiebangFuwuZhan(){
	   var ids = '';
    $('.ids').each(function() {
        if (this.checked == true) {
            ids += 'ids=' + this.value + '&';
        }
    });
    if (ids.length == 0) {
        showDialog('请至少选择一条记录！');
        return;
    }
    $('#commonDialogDiv').html('<p>确定要解除平板的绑定信息？</p>');
    $('#commonDialogDiv').dialog( {
        modal : true,
        width : 250,
        height : 160,
        buttons : {
            '确定' : function() {
                $(this).dialog('close');
                $.ajax( {
                    url : 'fuwuZhan/updateJiebang.do?' + ids,
                    type : 'get',
                    dataType : 'json',
                    success : function(data) {
                        if (data.success) {
                            showDialog('解绑成功');
                        } else {
                            showDialog('解绑失败，原因：' + data.message);
                        }
                    }
                });
            },
            '取消' : function() {
                $(this).dialog('close');
            }
        },close:function(){$(this).dialog('destroy');}
    });
}